import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    state:{
        firstLogin:0,//0不是第一次登录，1是第一次登录
        cartFoods:{},//购物车中食物的列表
        cartMessage:{
            count:0,
            sum:0
        },
        foodList:[],//食物菜单列表
        chooseAddress:{},//获取到的暂存地址
        reduction:{},//记录优惠的信息:满减/百亿
        historyUrl:'userInformation',//记录进入新增地址的前一个路径
        address:{},//用户的收获地址
        shopName:"",//当前店铺的名字
        distribution:{},//当前店铺的原始配送费,里面记录配送费和减去的配送费
        modifyId:"",//当前修改地址的地址ID
        redBag:{},//当前选择的红包对象
        shopId:"",//当前进入的店铺Id
        addAddressMessage:{},//增加地址的信息暂存
    },
    mutations:{
        addFoodList(state,arr){
            state.foodList.push(arr);
        },
        addFoodCount(state,getobj){//增加食物的数量
            if(state.cartFoods[getobj.food]==undefined){
                //第一个增加的时候，food.count=1,新增加属性没有数据绑定
                //set(对象，属性名，值)，让新增属性也有数据绑定
                let obj={};
                Vue.set(obj,"foodName",getobj.name);
                Vue.set(obj,"foodUrl",getobj.url);
                Vue.set(obj,"count",1);
                Vue.set(obj,"foodMoney",getobj.money);
                Vue.set(obj,"foodPack",getobj.packcost);
                Vue.set(state.cartFoods,getobj.food,obj);
            }else state.cartFoods[getobj.food].count=state.cartFoods[getobj.food].count+1;
            state.cartMessage.count=state.cartMessage.count+1;
            state.cartMessage.sum=state.cartMessage.sum+getobj.money+getobj.packcost;
        },
        clearFoodList(state){
            state.foodList=[];
        },
        setCartFoods(state,obj){
            state.cartFoods={};
            state.cartFoods=obj;
        },
        setCartMessage(state,obj){
            state.cartMessage={};
            state.cartMessage=obj;
        },
        subFoodCount(state,getobj){//减少食物的数量
            if(state.cartFoods[getobj.food]!=undefined){
                state.cartFoods[getobj.food].count=state.cartFoods[getobj.food].count-1;
                if(state.cartFoods[getobj.food].count==0)
                    // 将food从cartFoods移除
                    delete state.cartFoods[getobj.food];
                    state.cartMessage.count=state.cartMessage.count-1;
                    state.cartMessage.sum=state.cartMessage.sum-getobj.money-getobj.packcost;
            }
        },
        clearCart(state){//清除购物车
            for(let obj in state.cartFoods){
                delete state.cartFoods[obj];
            }
            // 清空购物车所有项
            state.cartFoods = {};
            state.cartMessage.count=0;
            state.cartMessage.sum=0;
        },
        setAddress(state,obj){//修改暂存新增地址
            state.chooseAddress=obj;
        },
        setReduction(state,obj){
            state.reduction[obj.name]=obj.value;
        },
        setHistory(state,obj){
            state.historyUrl=obj;
        },
        setAddressList(state,obj){
            state.address=obj;
        },
        setShopName(state,obj){
            state.shopName=obj;
        },
        setModifyId(state,obj){
            state.modifyId=obj;
        },
        setDistribution(state,obj){
            state.distribution=obj;
        },
        setRedBag(state,obj){
            state.redBag=obj;
        },
        setFirstLogin(state,obj){
            state.firstLogin=obj;
        },
        setShopId(state,obj){
            state.shopId=obj;
        },
        initAddAddressMessage(state){//初始化增加地址的暂存信息
            Vue.set(state.addAddressMessage,'name',"");
            Vue.set(state.addAddressMessage,'phoneName',"");
            Vue.set(state.addAddressMessage,'sex',"1");
            Vue.set(state.addAddressMessage,'doorId',"");
            Vue.set(state.addAddressMessage,'tag',"1");
        },
        setAddAddressMessage(state,obj){
            state.addAddressMessage=obj;
        }
    },
    actions:{
         // 同步更新food的count
        updateFoodCount(context,{isAdd,food,name,url,money,packcost}) {
            if (isAdd) {
                context.commit('addFoodCount',{food,name,url,money,packcost});
            } else {
                context.commit('subFoodCount',{food,name,url,money,packcost});
            }
        }
    },
    getter:{
    }
})